package com.example.eldersystem.operatedata.mapper;

import com.example.eldersystem.operatedata.model.SystemUser;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface SystemUserMapper {
    @Insert("insert into sys_user(user_name,real_name,password,sex,phone,email,description,logoimage)" +
            "values (#{userName},#{realName},#{password},#{sex},#{phone},#{email},#{description},#{logoImage}) ")
    int addSystemUser(SystemUser systemUser);

    @Update("update sys_user " +
            "set user_name=#{userName}," +
            "real_name=#{realName}," +
            "password=#{password}," +
            "sex=#{sex}," +
            "phone=#{phone}," +
            "email=#{email}," +
            "description=#{description}, " +
            "logoimage=#{logoImage} " +
            "where id = #{id}")
    int updateSystemUser(SystemUser systemUser);

    @Update("update sys_user set logoimage=#{logoImage} where id=#{id}")
    int updateLogoImage(int id,String logoImage);

    @Delete("delete from sys_user where id = #{id}")
    int deleteSystemUser(int id);

    @Select("select count(*) from sys_user where user_name=#{userName}")
    int selectSystemUserByUserName(String userName);

    @Select("select count(*) from sys_user")
    int selectSystemUserNum();

    @Select("select * from sys_user limit #{from},#{num}")
    @Results(id="select1",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "userName",column = "user_name"),
            @Result(property = "realName",column = "real_name"),
            @Result(property = "password",column = "password"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "email",column = "email"),
            @Result(property = "description",column = "description"),
            @Result(property = "logoImage",column = "logoimage")
    })
    List<SystemUser> selectSystemUser(int from, int num);

    @Select("select count(*) from sys_user where real_name like concat('%',#{realName},'%')")
    int selectSystemUserNum2(String realName);

    @Select("select * from sys_user where real_name like concat('%',#{realName},'%')")
    @Results(id="select2",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "userName",column = "user_name"),
            @Result(property = "realName",column = "real_name"),
            @Result(property = "password",column = "password"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "email",column = "email"),
            @Result(property = "description",column = "description"),
            @Result(property = "logoImage",column = "logoimage")
    })
    List<SystemUser> selectSystemUserByRealName(String realName);


    @Select("select ifnull(max(id),0) from sys_user where user_name=#{userName} and password=#{password}")
    int login(String userName,String password); //if exists,return id; if not,return 0

    @Select("select * from sys_user where id=#{id}")
    @Results(id="select3",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "userName",column = "user_name"),
            @Result(property = "realName",column = "real_name"),
            @Result(property = "password",column = "password"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "email",column = "email"),
            @Result(property = "description",column = "description"),
            @Result(property = "logoImage",column = "logoimage")
    })
    SystemUser getCurrentUser(int id);

}
